#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
#endregion

namespace Thn.Communication.Exceptions
{
    /// <summary>
    /// 
    /// </summary>
    public class ConfigurationException : Thn.Communication.Exceptions.CommunicationExceptionBase
    {
        #region Get Message
        /// <summary>
        /// Empty message builder
        /// </summary>
        static string GetMessage()
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = "Lỗi nạp tập tin cấu hình.";
            else
                result = "Error occured while parsing configuration file";
            return result;
        }
        
        static string GetMessage_NoTeleportSection()
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = string.Format("File cấu hình phải có ít nhất 1 section có ID (xml tag) là Teleport");
            else
                result = string.Format("Configuration file must contains at least 1 section with ID (xml tag) named Teleport");
            return result;
        }

        static string GetMessage_SectionNotFound(string sectionName)
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = string.Format("Không tìm thấy section nào có giá trị Name là [{0}]", sectionName);
            else
                result = string.Format("Could not find any section whose property Name is [{0}]", sectionName);
            return result;
        }

        static string GetMessage_NoConnectorName()
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = "Phải có tên của class dùng làm connector.";
            else
                result = "Name of the class used as the connector must be provided";
            return result;
        }

        static string GetMessage_ConnectorNotFound(string connector)
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = string.Format("Không tìm thấy class nào có giá tên là [{0}]", connector);
            else
                result = string.Format("Could not find any class with name [{0}]", connector);
            return result;
        }

        static string GetMessage_InterfaceNotFound(Type classType)
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = string.Format("Class [{0}] phải có interface ITeleportConnector", classType.Name);
            else
                result = string.Format("Class [{0}] must implement interface ITeleportConnector", classType.Name);
            return result;
        }
        #endregion

        #region Publish
        /// <summary>
        /// Publish exception when no section with ID is Teleport
        /// </summary>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_NoTeleportSection()
        {
            string message = GetMessage_NoTeleportSection();
            ConfigurationException error = new ConfigurationException(message);

            //write error to log            
            if (Thn.Communication.Exceptions.CommunicationExceptionBase.LogError) Thn.Log.Error(error);
            //throw error
            if (!Thn.Communication.Exceptions.CommunicationExceptionBase.SilentError) throw error;
        }

        /// <summary>
        /// Publish exception when could not find any teleport section with provided name
        /// </summary>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_SectionNotFound(string sectionName)
        {
            string message = GetMessage_SectionNotFound(sectionName);
            ConfigurationException error = new ConfigurationException(message);

            //write error to log            
            if (Thn.Communication.Exceptions.CommunicationExceptionBase.LogError) Thn.Log.Error(error);
            //throw error
            if (!Thn.Communication.Exceptions.CommunicationExceptionBase.SilentError) throw error;
        }

        /// <summary>
        /// Publish exception when name of the connector class is not provided
        /// </summary>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_NoConnectorName()
        {
            string message = GetMessage_NoConnectorName();
            ConfigurationException error = new ConfigurationException(message);

            //write error to log            
            if (Thn.Communication.Exceptions.CommunicationExceptionBase.LogError) Thn.Log.Error(error);
            //throw error
            if (!Thn.Communication.Exceptions.CommunicationExceptionBase.SilentError) throw error;
        }

        /// <summary>
        /// Publish exception when could not find the specified class name to use as the connector
        /// </summary>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_ConnectorNotFound(string connector)
        {
            string message = GetMessage_ConnectorNotFound(connector);
            ConfigurationException error = new ConfigurationException(message);

            //write error to log            
            if (Thn.Communication.Exceptions.CommunicationExceptionBase.LogError) Thn.Log.Error(error);
            //throw error
            if (!Thn.Communication.Exceptions.CommunicationExceptionBase.SilentError) throw error;
        }

        /// <summary>
        /// Publish exception when the connector class does not implement interface ITeleportConnector
        /// </summary>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_InterfaceNotFound(Type classType)
        {
            string message = GetMessage_InterfaceNotFound(classType);
            ConfigurationException error = new ConfigurationException(message);

            //write error to log            
            if (Thn.Communication.Exceptions.CommunicationExceptionBase.LogError) Thn.Log.Error(error);
            //throw error
            if (!Thn.Communication.Exceptions.CommunicationExceptionBase.SilentError) throw error;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Empty constructor
        /// </summary>
        public ConfigurationException()
            : base(GetMessage()) { }

        /// <summary>
        /// Create new instance
        /// </summary>        
        public ConfigurationException(string message)
            : base(message) { }
        #endregion
    }
}
